package com.local.admin.controller;


import com.local.common.util.ResultUtil;
import com.local.seckill.feign.SeckillServiceFeign;
import com.local.seckill.pojo.Seckill;
import com.local.seckill.pojo.SeckillListRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;


@Controller
@RequestMapping("/admin/seckill")
public class AdminSeckillController {

    @Autowired
    private SeckillServiceFeign seckillServiceFeign;

    @GetMapping
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "seckill");
        return "admin/seckill";
    }


    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam("sidx") String sort, @RequestParam("order") String order,
                           @RequestParam(name = "goodsId", required = false) Integer goodsId,
                           @RequestParam(name = "seckillStatus", required = false) Integer seckillStatus,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime) {
        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        limit = Objects.isNull(limit) || limit <= 0 ? 1 : limit;
        sort = Objects.isNull(sort) || sort.isEmpty() ? "seckill_id" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;

        SeckillListRequestParam requestParam = new SeckillListRequestParam(page, limit, sort, order, goodsId, seckillStatus, startTime, endTime, -1);
        ResultUtil listResult = seckillServiceFeign.seckillList(requestParam);
        return listResult;
    }


    /**
     * 根据id查找数据
     * @param seckillId
     * @return
     */
    @GetMapping("/{seckillId}")
    @ResponseBody
    public ResultUtil getById(@PathVariable("seckillId") Integer seckillId) {
        ResultUtil seckillResult = seckillServiceFeign.getSeckillById(seckillId);
        return seckillResult;
    }


    /**
     * 新增
     * @param seckill
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Seckill seckill){
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        seckill.setCreateTime(dateTime);
        seckill.setUpdateTime(dateTime);
        seckill.setIsDeleted(0);
        ResultUtil saveResult = seckillServiceFeign.seckillSave(seckill);
        return saveResult;
    }


    /**
     * 修改
     * @param seckill
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Seckill seckill){
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        seckill.setUpdateTime(dateTime);
        seckill.setIsDeleted(0);
        ResultUtil updateResult = seckillServiceFeign.seckillUpdate(seckill);
        return updateResult;
    }


    /**
     * 根据id逻辑删除
     * @param seckillId
     * @return
     */
    @DeleteMapping("/{seckillId}")
    @ResponseBody
    public ResultUtil delete(@PathVariable("seckillId") Integer seckillId) {
        ResultUtil deleteResult = seckillServiceFeign.seckillDelete(seckillId);
        return deleteResult;
    }
}


